package com.usian.controller.shop;

import com.usian.pojo.login.Login;
import com.usian.pojo.shop.Shop;
import com.usian.pojo.user.User;
import com.usian.service.FunctionService;
import com.usian.service.ShopService;
import com.usian.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("shop")
public class ShopController {

    @Resource
    private UserService userService;

    @Resource
    private FunctionService functionService;

    @Resource
    private ShopService shopService;

    /*@RequestMapping("/Choose")
    public String Choose(Integer userId, HttpSession session, Model model) {

        //得到userId对应的user
        User user = userService.getById(userId);

        Integer rid = userService.decideRidByUid(user.getUid());

        //得到function表里的对应id
        List<Integer> fidList = functionService.decideFidByRid(rid);

        Login login = new Login(userId, user.getUserName(),rid,fidList);

        session.setAttribute("login", login);

        return this.findAll(model, session);
    }*/

    /**
     * 查看角色对应的数据
     * */
    @RequestMapping("/findAll")
    public String findAll(Model model, HttpSession session) {

        Login login = (Login) session.getAttribute("login");

        //判断角色  运营-1    商品中心管理员-2
        //商品状态 state 0-新增/修改 1-启用  3-提交审核/待审核  5-审核通过  7-上架  9-下架  11-审核失败
        //运营只能查询  商品状态 小于提交审核的商品信息
        //商品中心管理员只能查询   商品状态 大于待审核状态后的商品数据
        if (login.getRoleId() == 1) {
            List<Shop> shopList = userService.listByStateLt3();
            model.addAttribute("shopList", shopList);
        } else {
            List<Shop> shopList = userService.listByStateGt5();
            model.addAttribute("shopList", shopList);
        }
        return "show/show";
    }

    @RequestMapping("/toInsert")
    public String toInsert(){
        return "shop/addShop";
    }

    @RequestMapping("/insert")
    public String insert(Shop shop){
        //将新增/修改的数据的 state=0
        shop.setState(0);
        //存入数据库
        shopService.save(shop);
        return "redirect:/shop/findAll";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer sid,Model model){
        Shop shop = shopService.getById(sid);
        model.addAttribute("shop",shop);
        return "shop/updateShop";
    }

    @RequestMapping("/update")
    public String update(Shop shop){
        //将新增/修改的数据的 state=0
        shop.setState(0);
        //存入数据库
        shopService.updateById(shop);
        return "redirect:/shop/findAll";
    }
}
